package top.humbleyuan.linkedlist;

import top.humbleyuan.datastructure.linkedlist.LinkNode;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 22:51
 * @Des 删去总和值为0的连续节点
 */
public class LeetCode_1171 {
    public static void main(String[] args) {
        /**
         * 数据准备
         */
        LinkNode head = new LinkNode(1);
        int[] vals = {2,3,-3,4};
        head = LinkNode.formLink(head,vals);

        LinkNode root = new LinkNode(-1);
        LinkNode preTemp = root;
        root.next = head;

        LinkNode firstCycle = head;
        while (firstCycle != null) {
            LinkNode secondCycle = firstCycle;
            int sum = firstCycle.val;

            while (sum != 0 && secondCycle.next != null) {
                secondCycle = secondCycle.next;
                sum += secondCycle.val;
            }

            if(sum == 0) {
                preTemp.next = secondCycle.next;
                firstCycle = secondCycle.next;
                continue;
            }
            preTemp = firstCycle;
            firstCycle = firstCycle.next;
        }

        LinkNode.headToString(root.next);
    }
}
